<template>
  <div class="finally">
    <div class="header">
      <van-icon name="arrow-left" class="icon" @click="goBack" />
      <div>排行榜</div>
    </div>
    <van-pull-refresh v-model="state.refreshing" @refresh="onRefresh">
      <van-list
        offset="5"
        error-text="请求失败，点击重新加载"
        v-model:error="state.error"
        v-model:loading="state.loading"
        :finished="state.finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <FinallyList :infoList="list"></FinallyList>

        <!-- <van-swipe-cell v-for="item in list" :key="item.id">
          <slot name="userInfo" :item="item"></slot>
          <template #right>
            <van-button @click="turn(item)" square type="danger" text="驳回注册" />
            <van-button @click="confirm(item)" square type="primary" text="同意注册" />
          </template>
        </van-swipe-cell> -->
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { onMounted, reactive, ref } from "@vue/runtime-core";
import { useRouter } from "vue-router";
import FinallyList from "../../components/Finally/FinallyList.vue";
import { getStudentScore, getTeacherScore } from "../../network/axios";
import { Notify } from "vant";
export default {
  name: "Header",
  components: {
    FinallyList,
  },
  setup() {
    const router = useRouter();
    const state = reactive({
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      error: false,
    });
    let parameObject = reactive({
      page: 0,
      limit: 10,
    });
    let list = ref([]);
    const goBack = () => {
      // router.go(-1);
      router.push({
        name: "LoginIn",
      });
    };

    const onLoad = () => {
      state.loading = true;
      parameObject.page++;
      getUserList();
    };
    const onRefresh = () => {
      parameObject.page = 1;
      state.finished = false;
      state.loading = true;
      getUserList("pull");
    };
    const getUserList = async (type) => {
      const types = localStorage.getItem("type");

      const {
        data: { result, count, code },
      } =
        types == 1
          ? await getStudentScore(parameObject)
          : await getTeacherScore(parameObject);
      if (code == 200) {
        if (type === "pull") {
          list.value = result;
          state.refreshing = false;
        } else {
          list.value.push(...result);
          if (list.value.length >= count) {
            state.finished = true;
          }
        }
        state.loading = false;
      } else {
        Notify({
          type: "danger",
          message: "获取排名错误，请稍后再试",
        });
      }
    };
    return {
      goBack,
      list,
      // counts,
      state,
      onRefresh,
      onLoad,
    };
  },
};
</script>
<style lang="scss" scoped>
.van-pull-refresh {
  flex: 1;
  overflow-y: scroll;
}
.finally {
  width: 100%;
  height: 100%;
  background-color: #c4e3fe;
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow-y: scroll;
  .header {
    //   position: fixed;
    width: 100%;
    height: 1rem;
    position: relative;
    text-align: center;
    line-height: 1rem;
    background: #202a5a;
    color: #fff;
    font-size: 0.35rem;
    //   top: 0;
    .icon {
      font-size: 0.5rem !important;
      position: absolute;
      left: 0.2rem;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}
</style>
